<?php

namespace Biz\service\Album;

use Biz\model\Album\AlbumListModel;
use Biz\model\Album\AlbumRelateTagsModel;
use Doraemon\model\NewTag\PackageApplication;
use Doraemon\model\NewTag\TagPackage;


class AlbumTagListService
{
    /**
     * 新增或修改清单标签
     * @param $albumId
     * @param $tagList
     * @param $adminId
     */
    public static function saveAlbumTagList($albumId, $tagList, $adminId)
    {
        if ($tagList) {
            //先全部删除
            AlbumRelateTagsModel::updateDataByWhere(['status' => AlbumRelateTagsModel::STATUS_DELETE],
                ['album_id' => $albumId]);
            //查询该清单已有的tag列表
            $tagArr = AlbumRelateTagsModel::selectByWhere(['album_id' => $albumId], 'tag_name');
            //再添加
            foreach ($tagList as $item) {
                $packageName = explode('|', $item)[0];
                $tagName = explode('|', $item)[1];
                if (in_array($tagName, $tagArr)) {
                    AlbumRelateTagsModel::updateDataByWhere([
                        'status' => AlbumRelateTagsModel::STATUS_NORMAL,
                        'package_name' => $packageName,
                        'admin_id' => $adminId,
                    ],
                        ['album_id' => $albumId, 'tag_name' => $tagName]);
                } else {
                    AlbumRelateTagsModel::insertData([
                        'album_id' => $albumId,
                        'package_name' => $packageName,
                        'tag_name' => $tagName,
                        'admin_id' => $adminId,
                    ]);
                }

            }
        }
    }


    //
    public static function getListFormat()
    {
        //查询 标签包 用户为好物清单的id
        $applicationId = PackageApplication::getInfoByName(AlbumListModel::TAG_APPLICATION_NAME, 'id');
        if (!$applicationId) {
            return [];
        }
        $tagArr = TagPackage::select("*", ['application_id' => $applicationId]);
        $result = [];
        if ($tagArr) {
            foreach ($tagArr as $key => $tagInfo) {
                $map = [
                    'id' => $tagInfo['id'],
                    'name' => $tagInfo['description'],
                    'level' => (string)1,
                    'children' => []
                ];
                $result[$key] = $map;
                $tags = explode(",", $tagInfo['tags']);
                $tagIds = explode(",", $tagInfo['tag_ids']);
                foreach ($tags as $idKey => $val) {
                    $result[$key]['children'][] = [
                        'id' => $tagIds[$idKey],
                        'level' => (string)2,
                        'name' => $val,
                        'children' => []

                    ];
                }
            }
        }
        return $result;
    }


    public static function getListFormatKey()
    {
        $applicationId = PackageApplication::getInfoByName(AlbumListModel::TAG_APPLICATION_NAME, 'id');
        if (!$applicationId) {
            return [];
        }
        $tagArr = TagPackage::select("*", ['application_id' => $applicationId]);
        if ($tagArr) {

            foreach ($tagArr as $tagInfo) {

                $result[$tagInfo['id']] = $tagInfo['description'];
                $tags = explode(",", $tagInfo['tags']);
                $tagIds = explode(",", $tagInfo['tag_ids']);
                foreach ($tags as $idKey => $val) {
                    $result[$tagIds[$idKey]] = $val;
                }
            }
        }


        return $result;
    }

    public static function getLabelByIds($albumIds)
    {
        $listKey = self::getListFormatKey();
        $key = array_combine(array_values($listKey), array_keys($listKey));
        $tagInfo = AlbumRelateTagsModel::selectByWhere([
            'album_id' => $albumIds,
            'status' => AlbumRelateTagsModel::STATUS_NORMAL
        ]);
        $result = [];
        if ($tagInfo && is_array($tagInfo)) {
            foreach ($tagInfo as $val) {
                $result[$val['album_id']][] = [
                    'id' => $val['id'],
                    'first_id' => (string)$key[$val['package_name']],
                    'second_id' => (string)$key[$val['tag_name']],
                    'source' => 2
                ];

            }
        }
        return $result;
    }


    public static function getTypesByObjIds($albumIds)
    {
        $listKey = self::getListFormatKey();
        $key = array_combine(array_values($listKey), array_keys($listKey));
        $tagInfo = AlbumRelateTagsModel::selectByWhere([
            'album_id' => $albumIds,
            'status' => AlbumRelateTagsModel::STATUS_NORMAL
        ]);
        $result = [];
        foreach ($tagInfo as $val) {
            $result[$val['album_id']] = [
                'id' => $val['id'],
                'obj_id' => $val['album_id'],
                'category_first_id' => $key[$val['package_name']],
                'category_second_id' => $key[$val['tag_name']],
                'category_first_str' => $val['package_name'],
                'category_second_str' => $val['tag_name']
            ];
        }
        return $result;
    }

    //给清单打标签

    public static function changeAlbumLabel($album_id, $firstId, $secondId, $admin_id)
    {
        $listKey = self::getListFormatKey();
        $secondIds = json_decode($secondId, true) ?: [];
        AlbumRelateTagsModel::updateDataByWhere([
            'status' => AlbumRelateTagsModel::STATUS_DELETE,
            'admin_id' => $album_id,
        ], ['album_id' => $album_id, 'package_name' => $listKey[$firstId],]);
        if (empty($secondIds)) {
            return [];
        }
        foreach ($secondIds as $val) {
            if ($val['id']) {
                $rs = AlbumRelateTagsModel::updateDataByWhere([
                    'status' => AlbumRelateTagsModel::STATUS_NORMAL,
                    'admin_id' => $album_id,
                ], ['id' => $val['id']]);
                $result = [
                    'id' => $val['id'],
                    'first_id' => (string)$firstId,
                    'second_id' => (string)$val['second_id'],
                    'source' => (string)2
                ];
            } else {
                $package_name = $listKey[$firstId];
                $tag_name = $listKey[$val['second_id']];
                $insertData = [
                    'album_id' => $album_id,
                    'package_name' => $package_name,
                    'tag_name' => $tag_name,
                    'admin_id' => $admin_id
                ];
                $rs = AlbumRelateTagsModel::insertData($insertData);
                $result = [
                    'id' => $rs,
                    'first_id' => (string)$firstId,
                    'second_id' => (string)$val['second_id'],
                    'source' => (string)2
                ];
            }
        }

        $tagNameArr = AlbumRelateTagsModel::selectByWhere(['album_id' => $album_id, 'status' => AlbumRelateTagsModel::STATUS_NORMAL], 'tag_name');
        $tags = $tagNameArr ? implode(',', $tagNameArr) : '';
        AlbumListModel::updateDataByWhere(['tags' => $tags], ['album_id' => $album_id]);
        return $result;
    }

    /**
     * @param $albumId
     * @param $adminId
     * @return bool
     */
    public static function delAlbumRelateTags($albumId, $adminId): bool
    {
        AlbumRelateTagsModel::updateDataByWhere(['status' => AlbumRelateTagsModel::STATUS_DELETE, 'admin_id' => $adminId], ['album_id' => $albumId]);
        return true;
    }

}